<template>
  <div class="app-wrapper" :class="classObj">
    <!-- 侧边栏 -->
    <div v-if="device === 'mobile' && sidebar.opened" class="drawer-bg" @click="handleClickOutside" />
    <double-sidebar class="sidebar-container" />

    <!-- 主内容区域 -->
    <div class="main-container">
      <!-- 顶部导航 -->
      <div class="fixed-header">
        <navbar />
      </div>
      
      <!-- 标签页导航 -->
      <tags-view v-if="needTagsView" />
      
      <!-- 页面内容 -->
      <app-main />
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useAppStore } from '@/stores/app'
import DoubleSidebar from './components/Sidebar/DoubleSidebar.vue'
import Navbar from './components/Navbar.vue'
import AppMain from './components/AppMain.vue'
import TagsView from './components/TagsView/index.vue'

const appStore = useAppStore()

const sidebar = computed(() => appStore.sidebar)
const device = computed(() => appStore.device)
const needTagsView = computed(() => true) // 可以配置是否需要标签页

const classObj = computed(() => ({
  hideSidebar: !sidebar.value.opened,
  openSidebar: sidebar.value.opened,
  withoutAnimation: sidebar.value.withoutAnimation,
  mobile: device.value === 'mobile'
}))

const handleClickOutside = () => {
  appStore.closeSidebar(false)
}
</script>

<style lang="scss" scoped>
@use '@/styles/variables.scss' as *;

.app-wrapper {
  position: relative;
  height: 100%;
  width: 100%;

  &.mobile.openSidebar {
    position: fixed;
    top: 0;
  }
}

.drawer-bg {
  background: #000;
  opacity: 0.3;
  width: 100%;
  top: 0;
  height: 100%;
  position: absolute;
  z-index: 999;
}

.fixed-header {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 9;
  width: calc(100% - 260px); // 60px (primary) + 200px (secondary)
  transition: width 0.28s;
}

.hideSidebar .fixed-header {
  width: calc(100% - 60px); // 只有一级侧边栏宽度
}

.mobile .fixed-header {
  width: 100%;
}

.main-container {
  min-height: 100%;
  transition: margin-left 0.28s;
  margin-left: 260px; // 60px (primary) + 200px (secondary)
  position: relative;
}

.sidebar-container {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 1001;
  overflow: hidden;
  display: flex;
  transition: all 0.28s;
  width: 260px; // 60px (primary) + 200px (secondary)
}

.hideSidebar .main-container {
  margin-left: 60px; // 只有一级侧边栏宽度
}

.hideSidebar .sidebar-container {
  width: 60px; // 只有一级侧边栏宽度
}

.sidebar-container .scrollbar-wrapper {
  overflow-x: hidden !important;
}

.el-scrollbar__bar.is-vertical {
  right: 0px;
}

.el-scrollbar__bar.is-horizontal {
  display: none;
}

// mobile responsive
.mobile {
  .main-container {
    margin-left: 0px;
  }

  .sidebar-container {
    transition: transform 0.28s;
  }

  &.hideSidebar {
    .sidebar-container {
      pointer-events: none;
      transition-duration: 0.3s;
      transform: translate3d(-260px, 0, 0);
    }
  }
}

.withoutAnimation {
  .main-container,
  .sidebar-container {
    transition: none;
  }
}
</style>
